<?

//require_once(dirname(__FILE__) . '/../abstract/sms.php');

class bf_sms_smsc extends bf_sms {

	protected $opts;
	protected $log;

	public function __construct($options = array()) {
		$this->opts = array_merge(array(
			'user' => 'smscuser',
			'password' => 'smscpass',
			'source-address' => false,
			'log' => dirname(__FILE__) . '/../../../logs/smsc.log',
			'logging' => 1,
			'schema' => 'https',
		), $options);
		$this->log = new bf_log($this->opts['log']);
	}

	public function get_balance() {
		$res = bf::$http->get($this->opts['schema'] . '://smsc.ru/sys/balance.php', array(
			'login' => $this->opts['user'],
			'psw' => $this->opts['password'],
			'fmt' => 3,
		));
		if ($this->opts['logging'] == 2) {
			$this->log->append(''
				. "-----------------------------------------\n"
				. "request BALANCE\n"
				. "request date: " . date('Y-m-d H:i:s') . "\n"
				. "response status: {$res['status']}\n"
				. "response text:\n{$res['response']}\n"
			);
		} else if ($this->opts['logging'] == 1) {
			$this->log->append(date('Y-m-d H:i:s') . ' INFO get balance (login = ' . $this->opts['user'] . ")\n");
		}
		if ($data = @json_decode($res['response'], true)) {
			if (!isset($data['error']) && isset($data['balance'])) {
				return sprintf('%.02f', $data['balance']);
			}
		}
		return null;
	}

	public function send($recipient, $text) {
		$params = array(
			'login' => $this->opts['user'],
			'psw' => $this->opts['password'],
			'phones' => $recipient,
			'mes' => $text,
			'charset' => 'utf-8',
			'fmt' => 3,
		);
		if ($this->opts['source-address']) {
			$params['sender'] = $this->opts['source-address'];
		}
		$res = bf::$http->get($this->opts['schema'] . '://smsc.ru/sys/send.php', $params);
		if ($this->opts['logging'] == 2) {
			$this->log->append(''
				. "-----------------------------------------\n"
				. "request SEND (rcp = $recipient, text = $text)\n"
				. "request date: " . date('Y-m-d H:i:s') . "\n"
				. "response status: {$res['status']}\n"
				. "response text:\n{$res['response']}\n"
			);
		} else if ($this->opts['logging'] == 1) {
			$this->log->append(date('Y-m-d H:i:s') . ' INFO send (login = ' . $this->opts['user'] . ", rcp = $recipient, text = $text)\n");
		}
		if ($data = @json_decode($res['response'], true)) {
			if (!isset($data['error']) && isset($data['id'])) {
				return $recipient . ':' . $data['id'];
			}
		}
		return null;
	}

	public function get_state($sms) {
		$parts = explode(':', $sms);
		$phone = @$parts[0];
		$sms_id = @$parts[1];
		$res = bf::$http->get($this->opts['schema'] . '://smsc.ru/sys/status.php', array(
			'login' => $this->opts['user'],
			'psw' => $this->opts['password'],
			'phone' => $phone,
			'id' => $sms_id,
			'fmt' => 3,
		));
		if ($this->opts['logging'] == 2) {
			$this->log->append(''
				. "-----------------------------------------\n"
				. "request STATUS (rcp = $phone, id = $sms_id)\n"
				. "request date: " . date('Y-m-d H:i:s') . "\n"
				. "response status: {$res['status']}\n"
				. "response text:\n{$res['response']}\n"
			);
		} else if ($this->opts['logging'] == 1) {
			$this->log->append(date('Y-m-d H:i:s') . ' INFO status (login = ' . $this->opts['user'] . ", rcp = $phone, id = $sms_id)\n");
		}
		if ($data = @json_decode($res['response'], true)) {
			if (!isset($data['error']) && isset($data['status'])) {
				return ($data['status'] == 1 ? true : false);
			}
		}
		return null;
	}
}

?>